package com.qf.shop.service.impl;

import com.qf.shop.dao.CartDao;
import com.qf.shop.dao.GoodsDao;
import com.qf.shop.dao.Impl.CartDaoImpl;
import com.qf.shop.dao.Impl.GoodsDaoImpl;
import com.qf.shop.entity.Cart;
import com.qf.shop.entity.Goods;
import com.qf.shop.service.CartService;
import com.qf.shop.utils.DruidUtils;

import java.util.List;

public class CartServiceImpl implements CartService {
    private CartDao cartDao = new CartDaoImpl();
    private GoodsDao goodsDao = new GoodsDaoImpl();

    @Override
    public int addCart(int id, int gid) {
        int result = 0;
        try {
            DruidUtils.begin();
            //1.判断 购物车当中商品是否已经添加
            //使用id,gid,在cart 表中进行查询  是否已经存在
            Cart cart = cartDao.existsCart(id,gid);
            // 1   1
            Goods goods = goodsDao.selectGoods(gid);
            if(cart!=null){
                //2.添加过，在当前数量上+1
                //数量+1
                cart.setNum(cart.getNum()+1);
                //money 也要根据数量而发生变化
                cart.setMoney(cart.getNum() * goods.getPrice());
                //存储商品信息
                cart.setGoods(goods);
                result = cartDao.updateCart(cart);
            }else{
                //3.没添加过，则添加新数据
                //使用gid 查询goods表，把添加的商品存在cart对象中
                Cart newCart = new Cart(id,gid,1,goods.getPrice());
                newCart.setGoods(goods);
                result = cartDao.insertCart(newCart);
            }
            DruidUtils.commit();
        } catch (Exception e) {
            DruidUtils.rollback();
            e.printStackTrace();
        }

        return result;
    }

    @Override
    public List<Cart> getCartById(int id) {
        List<Cart> carts = null;

        try {
            DruidUtils.begin();
            carts = cartDao.selectById(id);
            for(Cart cart :carts){
                Goods goods = goodsDao.selectGoods(cart.getGid());
                cart.setGoods(goods);
            }
            DruidUtils.commit();
        } catch (Exception e) {
            DruidUtils.rollback();
            e.printStackTrace();
        }

        return carts;
    }

    @Override
    public int deleteCartByGid(int gid) {
        int result = 0;

        try {
            DruidUtils.begin();
            result= cartDao.deleteCart(gid);
            DruidUtils.commit();
        } catch (Exception e) {
            DruidUtils.rollback();
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public int updateCart(Cart cart) {
        int result =0;
        try {
            DruidUtils.begin();
            result = cartDao.updateCart( cart);
            DruidUtils.commit();
        } catch (Exception e) {
            DruidUtils.rollback();
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public int clearCartById(int id) {
        int result = 0;
        try {
            DruidUtils.begin();
            result = cartDao.deleteById(id);
            DruidUtils.commit();
        } catch (Exception e) {
            DruidUtils.rollback();
            e.printStackTrace();
        }
        return result;
    }
}
